#include "Motor_FreeRtos_Task.h"

// 天问任务
// 设置接收数据缓冲区
uint8_t rx_len;
uint8_t tw51_rx_buf[100];
void TW51_FreeRtos_Task(void *args);
#define TW51_TASK_NAME     "TW51_task"
#define TW51_TASK_STACK    128
#define TW51_TASK_PRIORITY 3
TaskHandle_t tw_51_handle;
#define TW51_TASK_PERIOD 50
TW_51_enum motor_state = MOTOR_STOP;

// 电机任务
void Motor_FreeRtos_Task(void *args);
#define MOTOR_TASK_NAME     "Motor_task"
#define MOTOR_TASK_STACK    128
#define MOTOR_TASK_PRIORITY 3
TaskHandle_t motor_handle;
#define MOTOR_TASK_PERIOD 50
// 电机当前状态
TW_51_enum motor_status = MOTOR_STOP;
// 电机结构体
Motor_Struct motor_struct = {&htim4, TIM_CHANNEL_4, 0};

// 创建按键任务
void Key_FreeRtos_Task(void *args);
#define KEY_TASK_NAME     "Key_task"
#define KEY_TASK_STACK    128
#define KEY_TASK_PRIORITY 3
TaskHandle_t key_handle;
#define KEY_TASK_PERIOD 100
Remote_key remote_key;

// 创建OLED任务
void Oled_FreeRtos_Task(void *args);
#define OLED_TASK_NAME     "Oled_task"
#define OLED_TASK_STACK    128
#define OLED_TASK_PRIORITY 3
TaskHandle_t oled_handle;
#define OLED_TASK_PERIOD 2000

// 创建变量记录二氧化碳浓度
uint8_t CO_2NUM;
// 定义变量标识记录
uint8_t CO_Flag;
// 天问51任务
void TW51_FreeRtos_Task(void *args)
{

    uint32_t preTime = xTaskGetTickCount();
    while (1) {
        // printf("TW51_FreeRtos_Task2222222\r\n");
        // 通过串口循环接收数据
        HAL_UARTEx_ReceiveToIdle_IT(&huart3, tw51_rx_buf, 100);
        if (rx_len > 0) {
            if (tw51_rx_buf[0] == '0') {
                // 停止电机
                // printf("motor_stop\r\n");
                motor_status = MOTOR_STOP;
            } else if (tw51_rx_buf[0] == '1') {
                // 电机启动
                // printf("motor_start\r\n");
                motor_status = MOTOR_START;
            } else if (tw51_rx_buf[0] == '2') {
                // 电机加速向前转
                // printf("motor_forward\r\n");
                motor_status = MOTOR_FORWARD;
            } else if (tw51_rx_buf[0] == '3') {
                // 电机加速向后转
                // printf("motor_reverse\r\n");
                motor_status = MOTOR_REVERSE;
            }
        }
        rx_len = 0;
        // 延时
        // vTaskDelayUntil(&preTime, TW51_TASK_PERIOD);
        vTaskDelay(TW51_TASK_PERIOD);
    }
}

// 电机任务
void Motor_FreeRtos_Task(void *args)
{
    uint32_t preTime = xTaskGetTickCount();
    while (1) {
        // printf("Motor_FreeRtos_Task11111111\r\n");
        // 设置电机转速
        APP_Motor_SetSpeed(motor_status, &motor_struct);

        // Motor_485_SetSpeed(motor_status, 300);
        vTaskDelay(MOTOR_TASK_PERIOD);
    }
}
// 按键任务
void Key_FreeRtos_Task(void *args)
{
    while (1) {
        // printf("Key_task_Start4444444\r\n");
        // 扫描按键
        APP_Key_Scan_Task(&remote_key);

        vTaskDelay(KEY_TASK_PERIOD);
    }
}

// 显示任务
// uint8_t oled_statusflg = 0;
uint16_t motor_speedmin = 0;

void Oled_FreeRtos_Task(void *args)
{

    while (1) {
        // printf("Oled_Task_Start333333333\r\n");
        OLED_Clear();
        OLED_ShowWord(1, 3, 0);
        OLED_ShowWord(1, 6, 1);
        OLED_ShowWord(1, 9, 2);
        OLED_ShowWord(1, 12, 3);
        // OLED_Clear_Part(2, 0, 12);
        //  OLED_Clear_Part(3, 0, 100);
        if (motor_status == MOTOR_STOP) {
            OLED_ShowString(2, 3, "stop");
        } else if (motor_status == MOTOR_START) {
            OLED_ShowString(2, 3, "start");
        } else if (motor_status == MOTOR_FORWARD) {
            OLED_ShowString(2, 3, "forward");
        } else if (motor_status == MOTOR_REVERSE) {
            OLED_ShowString(2, 3, "reverse");
        }
        OLED_ShowString(3, 3, "speed:");
        if (motor_struct.speed < 0) {
            motor_speedmin = abs(motor_struct.speed);
            // OLED_Clear_Part(3, 9, 15);
            printf("%d", motor_speedmin);
            OLED_ShowString(3, 9, "-");
            OLED_ShowNum(3, 10, motor_speedmin, 3);
        } else {
            // OLED_Clear_Part(3, 9, 14);
            OLED_ShowNum(3, 11, motor_struct.speed, 3);
        }
        CO_2NUM++;
        OLED_ShowString(4, 3, "co2mol/L:");
        OLED_ShowNum(4, 12, CO_2NUM, 3);
        if (CO_2NUM > 10) {
            CO_Flag = 1;
        }

        // for (uint8_t i = 0; i < 7; i++)
        // {
        //    OLED_ShowChar(3, i+3, oled_charbuf[i]);
        // }

        vTaskDelay(OLED_TASK_PERIOD);
    }
}

// FreeRtos任务启动
void Motor_FreeRtos_Task_Start(void)
{
    // 初始化电机
    Motor_485_Init();
    // 初始化按键
    Int_key_init();
    // 初始化屏幕
    OLED_Init();
    // 创建天问51电机控制任务
    xTaskCreate(TW51_FreeRtos_Task, TW51_TASK_NAME, TW51_TASK_STACK, NULL, TW51_TASK_PRIORITY, &tw_51_handle);
    // 创建电机任务
    xTaskCreate(Motor_FreeRtos_Task, MOTOR_TASK_NAME, MOTOR_TASK_STACK, NULL, MOTOR_TASK_PRIORITY, &motor_handle);
    // 创建按键任务
    xTaskCreate(Key_FreeRtos_Task, KEY_TASK_NAME, KEY_TASK_STACK, NULL, KEY_TASK_PRIORITY, &key_handle);
    // 创建OLed任务
    xTaskCreate(Oled_FreeRtos_Task, OLED_TASK_NAME, OLED_TASK_STACK, NULL, OLED_TASK_PRIORITY, &oled_handle);

    // 启动任务调度器
    vTaskStartScheduler();
}